/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package iconeLibrary.model;

import iconeLibrary.controler.*;
import iconeLibrary.view.IconeLibraryServlet;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.xmldb.api.base.CompiledExpression;
import org.xmldb.api.base.Resource;
import org.xmldb.api.base.ResourceIterator;
import org.xmldb.api.base.ResourceSet;
import org.xmldb.api.base.XMLDBException;
import org.xmldb.api.modules.XQueryService;

/**
 *
 * @author Ayede Sonia :: Gaudino Anthony :: Herche Bilal :: Teixeira Tiago :: Touati Samed  :: Wadjahdi Marc
 */
public class XQueryRequest {

    private XQueryService xQueryService;

    /**
     * Method that create an XqueryRequest
     * @param xQueryService
     */
    public XQueryRequest(XQueryService xQueryService) {
        this.xQueryService = xQueryService;
    }

    /**
     * Method that execute a Xquery request .
     * @param xQueryPath
     * @param params
     * @return the result of the request .
     */
    public String executeRequeteXQuery(String xQueryPath, HashMap<String, Object> params) {
        try {
            String request = Controler.convertStreamToString(readXQueryFile(xQueryPath));
            CompiledExpression compiled = xQueryService.compile(request);
            if (params != null) {
                Iterator<String> it = params.keySet().iterator();
                while (it.hasNext()) {
                    String key = it.next();
                    Object value = params.get(key);
                    xQueryService.declareVariable(key, value);
                }
                params.clear();
            }
            ResourceSet result = xQueryService.execute(compiled);

            String res = "";

            ResourceIterator it = result.getIterator();
            while (it.hasMoreResources()) {
                Resource r = it.nextResource();
                res += (String) r.getContent();
            }
            return res;
        } catch (XMLDBException ex) {
            Logger.getLogger(IconeLibraryServlet.class.getName()).log(Level.SEVERE, null, ex);
        }catch (IOException ex) {
            Logger.getLogger(IconeLibraryServlet.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    /**
     * Method that read a XQueryFile .
     * @param xQueryFilePath path to the XQuery File .
     * @return a Reader
     */
    private Reader readXQueryFile(String xQueryFilePath) {
        try {
            InputStream in = this.getClass().getClassLoader().getResourceAsStream(xQueryFilePath);
            Reader xquery = new InputStreamReader(in);
            while (!xquery.ready()) {
            }
            return xquery;

        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
